package com.project.service;

import com.alibaba.fastjson.JSONObject;
import com.project.dao.UniversityMapper;
import com.project.entity.University;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WheelChen on 2017/5/4.
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class UniversityServiceImpl extends BaseService implements UniversityService {
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private AreaService areaService;


    private static UniversityMapper universityMapper = null;

    public void setMapper() {
        if (universityMapper == null)
            universityMapper = sqlSessionTemplate.getMapper(UniversityMapper.class);//1.获取相关mapper
    }

    @Override
    public int add(University university) {
        setMapper();
        return universityMapper.insertSelective(university);
    }

    @Override
    public int modify(University university) {
        setMapper();
        return universityMapper.updateByPrimaryKeySelective(university);
    }

    @Override
    public University findOne(Integer id) {
        setMapper();
        return universityMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017-5-2
     * function: 查找所有学校信息
     *
     * @return
     */
    @Override
    public List<University> findAll() {
        setMapper();
        return universityMapper.selectAll();
    }

    /**
     * author: WheelChen
     * date: 2017/7/24
     * function: 查找所有学校信息及其下属学院信息 + 条件查找
     *
     * @param university
     * @return
     */
    @Override
    public List<University> findUniversityAndSchoolsByCondition(University university) {
        setMapper();
        return universityMapper.selectUniversityAndSchoolsByCondition(university);
    }

    /**
     * author: WheelChen
     * date: 2017/9/20
     * function: 查找所有学校信息及其下属学院信息 + 条件查找（根据大学数量分页）
     *
     * @param university
     * @return
     */
    @Override
    public List<JSONObject> findUniversityAndSchoolsByCondition2(University university) throws Exception {
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<University> universityList = findByCondition(university);
        for (University university1: universityList){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("university", university1);
            jsonObject.put("area", areaService.findOneDetailById(university1.getLocationid()));
            jsonObject.put("schoolList", schoolService.findBatchWithAdminByUniversityId(university1.getId()));
            jsonObjectList.add(jsonObject);
        }
        return jsonObjectList;
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 模糊分页搜索
     *
     * @param university
     * @return
     */
    @Override
    public List<University> findByCondition(University university) {
        setMapper();
        return universityMapper.selectByCondition(university);
    }
}
